<script setup lang="ts">
import { db } from "../../../common/db";
import { ModelChat } from "../../../model/ModelChat";
import BarTop from "../../Component/BarTop.vue";

let insertData = async() =>{
  let model = new ModelChat()
  model.fromName = "聊天对象"
  model.sendTime = Date.now()
  model.lastMsg = "末尾追加的数据"
  model.isSelected = false
  model.avatar = 'https://pic3.zhimg.com/v2-306cd8f07a20cba46873209739c6395d_im.jpg?source=32738c0c'
  await db("Chat").insert(model)
}

let insertMultiData = async () => {
  let result = []
  for (let i = 0; i < 10; i++) {
    let model = new ModelChat()
    model.fromName = "聊天对象" + i
    model.sendTime = Date.now()
    model.isSelected = false
    model.lastMsg = "末尾追加的数据" + i
    model.avatar = 'https://pic3.zhimg.com/v2-306cd8f07a20cba46873209739c6395d_im.jpg?source=32738c0c'
    result.push(model)
  }
  result[5].isSelected = true
  await db("Chat").insert(result)
}

let selectData = async (fromName='聊天对象5') => {
  let data = await db("Chat").where({fromName}).first()
  console.log(JSON.stringify(data),'data ======== ')
}

let updateData = async ({fromName='聊天对象5',lastMsg},data) => {
  // form:{fromName,lastMsg}
  let result = await db("Chat").where({fromName}).update({lastMsg:"我错了😭 😞"})
}

let deleteData = async ({fromName='聊天对象3'}) => {
  await db("Chat").where({fromName}).delete()
}
</script>
<template>
  <div>联系人</div>
  <div class="ContactBoard">
    <BarTop />
    <div class="contentTest">
      <button @click="insertData">增加一行数据</button>

      <button @click="insertMultiData">增加多行数据</button>

      <button @click="selectData()">查询一行数据</button>

      <button @click="updateData">修改一行数据</button>

      <button @click="deleteData">删除一行数据</button>

      <br />

      <button @click="transaction">使用事务</button>
      <br />
      <button @click="getFirstPage()">获取第一页数据</button>
      <button @click="getNextPage()">获取下一页数据</button>
      <button @click="getPrevPage()">获取上一页数据</button>
      <button @click="getLastPage()">获取最后一页数据</button>
    </div>
  </div>
</template>
<style scoped lang="scss">
.ContactBoard {
  flex: 1;
}
</style>
